package com.sunwayworld.basemodule.business.folder.service.impl;

import com.sunwayworld.framework.mybatis.mapper.MapperParameter;
import com.sunwayworld.framework.support.PersistableHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.sunwayworld.framework.at.annotation.AuditTrailType;
import com.sunwayworld.framework.at.annotation.AuditTrailEntry;
import com.sunwayworld.framework.spring.annotation.GikamBean;

import com.sunwayworld.basemodule.business.folder.dao.LimsOrderDao;
import com.sunwayworld.basemodule.business.folder.bean.LimsOrderBean;
import com.sunwayworld.basemodule.business.folder.service.LimsOrderService;
import com.sunwayworld.framework.context.ApplicationContextHelper;
import com.sunwayworld.framework.restful.data.RestJsonWrapperBean;

import java.util.List;
import java.util.stream.Collectors;

@Repository
@GikamBean
public class LimsOrderServiceImpl implements LimsOrderService {

    @Autowired
    private LimsOrderDao limsOrderDao;

    @Override
    @SuppressWarnings("unchecked")
    public LimsOrderDao getDao() {
        return limsOrderDao;
    }

    @Override
    @Transactional
    @AuditTrailEntry(AuditTrailType.INSERT)
    public Long insert(RestJsonWrapperBean jsonWrapper) {
        LimsOrderBean limsOrder = jsonWrapper.parseUnique(LimsOrderBean.class);
        limsOrder.setId(ApplicationContextHelper.getNextIdentity());
        getDao().insert(limsOrder);
        return limsOrder.getId();
    }

    @Override
    public List<LimsOrderBean> selectLastTaskBySampleId(MapperParameter parameter) {
        return getDao().selectLastTaskBySampleId(parameter).parallelStream()
                .map(t -> PersistableHelper.mapToPersistable(t, LimsOrderBean.class)).collect(Collectors.toList());
    }
}
